const app = getApp();
const { AskComponent } = app._require("common/index");

AskComponent({
    properties: {
        type: {
            type: String,
            value: "image", // image || video || file
        },
        forDisplay: {
            type: Boolean,
            value: false,
        },
        fileList: {
            type: Array,
            value: [],
        },
        props: {
            type: Object,
            value: {
                dir: "",
                suffix: "",
                limit: 10 * 1024 * 1024,
                timeout: 30 * 1000,
            },
            observer: "checkProps",
        },
        drag: {
            type: Boolean,
            value: false,
        },
        action: {
            type: String,
            value: "",
        },
        uploadType:{
            type: String,
            value: "",
        },
    },
    data: {
        list: [],
        _props: {},
        // uploadType:""
    },
    methods: {
        checkProps() {
            const videoSuffix = ["ogv", "avi", "mp4", "m4v", "mpeg", "wmv", "mov", "ogm", "webm", "asx", "mpg"];
            const imageSuffix = [
                "png",
                "jpg",
                "jpeg",
                "dib",
                "webp",
                "svgz",
                "svg",
                "gif",
                "ico",
                "tif",
                "xbm",
                "bmp",
                "jfif",
                "pjpeg",
                "pjp",
                "tiff",
            ];
            const _props = {
                dir: "",
                suffix: "",
            };
            console.log(this.data.uploadType);
            _props.dir = "saas-applet/" + (this.data.props.dir || "");
            _props.timeout = this.data.props.timeout || 30 * 1000;
            if (this.data.type === "image") {
                _props.num = this.data.props.num || 0;
                _props.suffix = "png";
                _props.limit = this.data.props.limit || 10 * 1024 * 1024;
                if (this.data.props.suffix && imageSuffix.includes(this.data.props.suffix)) {
                    _props.suffix = this.data.props.suffix;
                }
            }
            if (this.data.type === "video") {
                _props.num = this.data.props.num || 1;
                _props.suffix = "mp4";
                _props.limit = this.data.props.limit || 50 * 1024 * 1024;
                if (this.data.props.suffix && videoSuffix.includes(this.data.props.suffix)) {
                    _props.suffix = this.data.props.suffix;
                }
            }
            if (this.data.type === "file") {
                _props.num = this.data.props.num || 0;
                _props.suffix = "";
                _props.limit = this.data.props.limit || 50 * 1024 * 1024;
                if (this.data.props.suffix) {
                    _props.suffix = this.data.props.suffix;
                }
            }
            this.setData({
                _props,
            });
        },
        updateOnItem(e) {
            this.triggerEvent("update", e.detail);
        },
        updateEnd(e) {

            // console.log("-----------",e)
            this.triggerEvent("updateEnd", e.detail);
        },
        upload(files) {

     
            if (this.data.type === "video") {
                this.selectComponent("#upload_video").upload(files);
            } else if (this.data.type === "file") {
                this.selectComponent("#upload_file").upload(files);
            } else {
                this.selectComponent("#upload_image").upload(files);
            }
        }
    },
});
